<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class ShareViewLogs_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_share_view_logs_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('share_view_logs', array('id'=>$id));
        return $query->row_array();
    }

    public function get_share_view_logs_by_uid($uid){
        $query = $this->db
            ->select('*')
            ->get_where('share_view_logs', array('user_id'=>$uid));
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改商家信息
     */
    public function sava_share_view_logs($id,$data){

        $this->db->where('id',$id);
        $this->db->update('share_view_logs', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商家
     */
    public function add_share_view_logs($data){

        $this->db->insert('share_view_logs',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有商家
     */
    public function get_all_share_view_logs(){
        $this->db->select('*');
        $this->db->from('share_view_logs');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_share_view_logs_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}share_view_logs  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商家
     */
    public function delete_share_view_logs_by($where){
        $this->db->where($where)->delete('share_view_logs');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_share_log_limit($uid,$good_id){
        $query = $this->db
            ->select('share_view_logs.*')
            ->from('share_view_logs')
            ->where('share_view_logs.user_id', $uid)
            ->where('share_view_logs.good_id', $good_id)
            ->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    }

}
